package cn.ac.iie.system.controller;

import cn.ac.iie.system.entity.Message;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice
public class RestExceptionController {

  @ExceptionHandler(value = SQLIntegrityConstraintViolationException.class)
  ResponseEntity<Message> constraintViolation(SQLIntegrityConstraintViolationException exception) {
    return new ResponseEntity<>(Message.builder().message(exception.getMessage()).build(), HttpStatus.BAD_REQUEST);
  }
}
